//
// Created by denglibin on 2021/6/2.
//

#include "game_window.h"
/**
 * 创建
 * @param width
 * @param height
 * @param title
 * @return
 */
GameWindow * GameWindowCreate(int width, int height, const char* title){
    //初始化，成功返回0
    if (SDL_Init(SDL_INIT_VIDEO)) {
        SDL_Log("Can not init video %s\n", SDL_GetError());
        exit(-1);
    }
    SDL_Window* win = SDL_CreateWindow(title,
                                       SDL_WINDOWPOS_CENTERED,
                                       SDL_WINDOWPOS_CENTERED,
                                       width, height,
                                       SDL_WINDOW_HIDDEN);
    if (win == NULL) {
        SDL_Log("Can not create window %s\n", SDL_GetError());
        return NULL;
    }
    GameWindow* window = (GameWindow*)malloc(sizeof(GameWindow));
    window->win_w = width;
    window->win_h = height;
    window->fps = 100;
    window->win = win;
    window->winRender = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
    window->winSurface = SDL_GetWindowSurface(win);

    window->mainSpirit = MainSpiritCreate(window->winRender);
    window->mainSpirit->x = width/2 - window->mainSpirit->w/2;
    window->mainSpirit->y = height/2 - window->mainSpirit->h/2;
    return window;
}

/**
 * 释放空间
 * @param window
 */
void GameWindowFree(GameWindow* gameWindow){
    SDL_DestroyRenderer(gameWindow->winRender);
    SDL_FreeSurface(gameWindow->winSurface);
    SDL_DestroyWindow(gameWindow->win);
    SDL_Quit();
    MainSpiritFree(gameWindow->mainSpirit);
    free(gameWindow);
}